/*   1:    */ package org.jetbrains.generate.tostring;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInsight.hint.HintManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.util.IncorrectOperationException;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.generate.tostring.config.*;
import org.jetbrains.generate.tostring.element.ClassElement;
import org.jetbrains.generate.tostring.element.ElementComparator;
import org.jetbrains.generate.tostring.element.ElementFactory;
import org.jetbrains.generate.tostring.element.ElementUtils;
import org.jetbrains.generate.tostring.exception.GenerateCodeException;
import org.jetbrains.generate.tostring.psi.PsiAdapter;
import org.jetbrains.generate.tostring.template.TemplateResource;
import org.jetbrains.generate.tostring.velocity.VelocityFactory;
import org.jetbrains.generate.tostring.view.MethodExistsDialog;

import java.io.StringWriter;
import java.util.*;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */
/*  47:    */
/*  48:    */
/*  49:    */
/*  50:    */
/*  51:    */
/*  52:    */
/*  53:    */
/*  54:    */
/*  55:    */ 
/*  56:    */ public class GenerateToStringWorker
/*  57:    */ {
/*  58: 50 */   private static final Logger a = Logger.getInstance("#org.jetbrains.generate.tostring.GenerateToStringWorker");
/*  59:    */   private final Editor b;
/*  60:    */   private final PsiClass c;
/*  61:    */   private final Config d;
/*  62:    */   private final boolean e;
/*  63:    */   
/*  64:    */   public GenerateToStringWorker(PsiClass paramPsiClass, Editor paramEditor, boolean paramBoolean)
/*  65:    */   {
/*  66: 58 */     this.c = paramPsiClass;
/*  67: 59 */     this.b = paramEditor;
/*  68: 60 */     this.d = GenerateToStringContext.getConfig();
/*  69: 61 */     this.e = paramBoolean;
/*  70:    */   }
/*  71:    */   
/*  72:    */   public void execute(Collection<PsiMember> paramCollection, TemplateResource paramTemplateResource)
/*  73:    */     throws IncorrectOperationException, GenerateCodeException
/*  74:    */   {
/*  75: 66 */     ConflictResolutionPolicy localConflictResolutionPolicy = exitsMethodDialog(paramTemplateResource);
/*  76:    */     
/*  77: 68 */     localConflictResolutionPolicy.setNewMethodStrategy(a(this.d.getInsertNewMethodInitialOption()));
/*  78:    */     
/*  79:    */ 
/*  80: 71 */     HashMap localHashMap = new HashMap();
/*  81:    */     
/*  82:    */ 
/*  83: 74 */     a(localHashMap, paramTemplateResource);
/*  84:    */     
/*  85:    */ 
/*  86: 77 */     PsiMethod localPsiMethod = a(paramCollection, localConflictResolutionPolicy, localHashMap, paramTemplateResource);
/*  87: 80 */     if (localPsiMethod != null) {
/*  88: 81 */       a(localPsiMethod, localHashMap, paramTemplateResource);
/*  89:    */     }
/*  90:    */   }
/*  91:    */   
/*  92:    */   private static InsertNewMethodStrategy a(InsertWhere paramInsertWhere)
/*  93:    */   {
/*  94: 86 */     switch (2.$SwitchMap$org$jetbrains$generate$tostring$config$InsertWhere[paramInsertWhere.ordinal()])
/*  95:    */     {
/*  96:    */     case 1: 
/*  97: 88 */       return InsertAfterEqualsHashCodeStrategy.getInstance();
/*  98:    */     case 2: 
/*  99: 90 */       return InsertAtCaretStrategy.getInstance();
/* 100:    */     case 3: 
/* 101: 92 */       return InsertLastStrategy.getInstance();
/* 102:    */     }
/* 103: 95 */     return InsertLastStrategy.getInstance();
/* 104:    */   }
/* 105:    */   
/* 106:    */   protected ConflictResolutionPolicy exitsMethodDialog(TemplateResource paramTemplateResource)
/* 107:    */   {
/* 108:107 */     DuplicationPolicy localDuplicationPolicy = this.d.getReplaceDialogInitialOption();
/* 109:108 */     if (localDuplicationPolicy == DuplicationPolicy.ASK)
/* 110:    */     {
/* 111:109 */       PsiMethod localPsiMethod = PsiAdapter.findMethodByName(this.c, paramTemplateResource.getTargetMethodName());
/* 112:110 */       if (localPsiMethod != null) {
/* 113:111 */         return MethodExistsDialog.showDialog(paramTemplateResource.getTargetMethodName());
/* 114:    */       }
/* 115:    */     }
/* 116:114 */     else if (localDuplicationPolicy == DuplicationPolicy.REPLACE)
/* 117:    */     {
/* 118:115 */       return ReplacePolicy.getInstance();
/* 119:    */     }
/* 120:119 */     return DuplicatePolicy.getInstance();
/* 121:    */   }
/* 122:    */   
/* 123:    */   private void a(Map<String, String> paramMap, TemplateResource paramTemplateResource)
/* 124:    */   {
/* 125:129 */     PsiMethod localPsiMethod = PsiAdapter.findMethodByName(this.c, paramTemplateResource.getTargetMethodName());
/* 126:130 */     if ((localPsiMethod != null) && (localPsiMethod.getDocComment() != null))
/* 127:    */     {
/* 128:131 */       PsiDocComment localPsiDocComment = localPsiMethod.getDocComment();
/* 129:132 */       if (localPsiDocComment != null) {
/* 130:133 */         paramMap.put("existingJavaDoc", localPsiDocComment.getText());
/* 131:    */       }
/* 132:    */     }
/* 133:    */   }
/* 134:    */   
/* 135:    */   @Nullable
/* 136:    */   private PsiMethod a(Collection<PsiMember> paramCollection, ConflictResolutionPolicy paramConflictResolutionPolicy, Map<String, String> paramMap, TemplateResource paramTemplateResource)
/* 137:    */     throws IncorrectOperationException, GenerateCodeException
/* 138:    */   {
/* 139:155 */     String str1 = a(paramCollection, paramMap, paramTemplateResource.getMethodBody());
/* 140:156 */     if (a.isDebugEnabled()) {
/* 141:156 */       a.debug("Method body generated from Velocity:\n" + str1);
/* 142:    */     }
/* 143:159 */     str1 = StringUtil.convertLineSeparators(str1);
/* 144:    */     
/* 145:    */ 
/* 146:162 */     JVMElementFactory localJVMElementFactory = JVMElementFactories.getFactory(this.c.getLanguage(), this.c.getProject());
/* 147:163 */     if (localJVMElementFactory == null) {
/* 148:164 */       return null;
/* 149:    */     }
/* 150:    */     PsiMethod localPsiMethod1;
/* 151:    */     try
/* 152:    */     {
/* 153:168 */       localPsiMethod1 = localJVMElementFactory.createMethodFromText(paramTemplateResource.getMethodSignature() + " { " + str1 + " }", this.c);
/* 154:169 */       CodeStyleManager.getInstance(this.c.getProject()).reformat(localPsiMethod1);
/* 155:    */     }
/* 156:    */     catch (IncorrectOperationException localIncorrectOperationException)
/* 157:    */     {
/* 158:171 */       HintManager.getInstance().showErrorHint(this.b, "'toString()' method could not be created from template '" + paramTemplateResource.getFileName() + '\'');
/* 159:    */       
/* 160:173 */       return null;
/* 161:    */     }
/* 162:177 */     PsiMethod localPsiMethod2 = this.c.findMethodBySignature(localPsiMethod1, false);
/* 163:178 */     PsiMethod localPsiMethod3 = paramConflictResolutionPolicy.applyMethod(this.c, localPsiMethod2, localPsiMethod1, this.b);
/* 164:179 */     if (localPsiMethod3 == null) {
/* 165:180 */       return null;
/* 166:    */     }
/* 167:183 */     if (this.e) {
/* 168:184 */       localPsiMethod3.getModifierList().addAnnotation("java.lang.Override");
/* 169:    */     }
/* 170:188 */     String str2 = (String)paramMap.get("existingJavaDoc");
/* 171:189 */     String str3 = paramTemplateResource.getJavaDoc();
/* 172:190 */     if ((str2 != null) || (str3 != null))
/* 173:    */     {
/* 174:192 */       str3 = a(paramCollection, paramMap, str3);
/* 175:193 */       if (a.isDebugEnabled()) {
/* 176:193 */         a.debug("JavaDoc body generated from Velocity:\n" + str3);
/* 177:    */       }
/* 178:195 */       a(localPsiMethod3, str2, str3);
/* 179:    */     }
/* 180:199 */     return localPsiMethod3;
/* 181:    */   }
/* 182:    */   
/* 183:    */   private static void a(PsiMethod paramPsiMethod, String paramString1, String paramString2)
/* 184:    */   {
/* 185:203 */     String str = paramString2 != null ? paramString2 : paramString1;
/* 186:204 */     PsiAdapter.addOrReplaceJavadoc(paramPsiMethod, str, true);
/* 187:    */   }
/* 188:    */   
/* 189:    */   private void a(PsiMethod paramPsiMethod, Map<String, String> paramMap, TemplateResource paramTemplateResource)
/* 190:    */   {
/* 191:217 */     PsiFile localPsiFile = this.c.getContainingFile();
/* 192:218 */     if ((localPsiFile instanceof PsiJavaFile))
/* 193:    */     {
/* 194:219 */       PsiJavaFile localPsiJavaFile = (PsiJavaFile)localPsiFile;
/* 195:220 */       if (paramMap.get("autoImportPackages") != null) {
/* 196:222 */         a(localPsiJavaFile, (String)paramMap.get("autoImportPackages"));
/* 197:    */       }
/* 198:    */     }
/* 199:225 */     paramPsiMethod = (PsiMethod)JavaCodeStyleManager.getInstance(this.c.getProject()).shortenClassReferences(paramPsiMethod);
/* 200:228 */     if ((!this.d.isJumpToMethod()) || (this.b == null)) {
/* 201:229 */       return;
/* 202:    */     }
/* 203:231 */     int i = paramPsiMethod.getTextOffset();
/* 204:232 */     if (i <= 2) {
/* 205:233 */       return;
/* 206:    */     }
/* 207:235 */     VisualPosition localVisualPosition = this.b.offsetToVisualPosition(i);
/* 208:236 */     if (a.isDebugEnabled()) {
/* 209:236 */       a.debug("Moving/Scrolling caret to " + localVisualPosition + " (offset=" + i + ")");
/* 210:    */     }
/* 211:237 */     this.b.getCaretModel().moveToVisualPosition(localVisualPosition);
/* 212:238 */     this.b.getScrollingModel().scrollToCaret(ScrollType.CENTER_DOWN);
/* 213:    */   }
/* 214:    */   
/* 215:    */   private static void a(PsiJavaFile paramPsiJavaFile, String paramString)
/* 216:    */     throws IncorrectOperationException
/* 217:    */   {
/* 218:248 */     StringTokenizer localStringTokenizer = new StringTokenizer(paramString, ",");
/* 219:249 */     while (localStringTokenizer.hasMoreTokens())
/* 220:    */     {
/* 221:250 */       String str = localStringTokenizer.nextToken().trim();
/* 222:251 */       if (a.isDebugEnabled()) {
/* 223:251 */         a.debug("Auto importing package: " + str);
/* 224:    */       }
/* 225:252 */       PsiAdapter.addImportStatement(paramPsiJavaFile, str);
/* 226:    */     }
/* 227:    */   }
/* 228:    */   
/* 229:    */   private String a(Collection<PsiMember> paramCollection, Map<String, String> paramMap, String paramString)
/* 230:    */     throws GenerateCodeException
/* 231:    */   {
/* 232:269 */     if (paramString == null) {
/* 233:270 */       return null;
/* 234:    */     }
/* 235:273 */     StringWriter localStringWriter = new StringWriter();
/* 236:    */     try
/* 237:    */     {
/* 238:275 */       VelocityContext localVelocityContext = new VelocityContext();
/* 239:    */       
/* 240:277 */       localVelocityContext.put("java_version", Integer.valueOf(PsiAdapter.getJavaVersion(this.c)));
/* 241:    */       
/* 242:    */ 
/* 243:280 */       a.debug("Velocity Context - adding fields");
/* 244:281 */       localVelocityContext.put("fields", ElementUtils.getOnlyAsFieldElements(paramCollection));
/* 245:    */       
/* 246:    */ 
/* 247:284 */       a.debug("Velocity Context - adding methods");
/* 248:285 */       localVelocityContext.put("methods", ElementUtils.getOnlyAsMethodElements(paramCollection));
/* 249:    */       
/* 250:    */ 
/* 251:288 */       a.debug("Velocity Context - adding members (fields and methods)");
/* 252:289 */       List localList = ElementUtils.getOnlyAsFieldAndMethodElements(paramCollection);
/* 253:291 */       if (this.d.getSortElements() != 0) {
/* 254:292 */         Collections.sort(localList, new ElementComparator(this.d.getSortElements()));
/* 255:    */       }
/* 256:294 */       localVelocityContext.put("members", localList);
/* 257:    */       
/* 258:    */ 
/* 259:297 */       ClassElement localClassElement = ElementFactory.newClassElement(this.c);
/* 260:298 */       localVelocityContext.put("class", localClassElement);
/* 261:299 */       if (a.isDebugEnabled()) {
/* 262:299 */         a.debug("Velocity Context - adding class: " + localClassElement);
/* 263:    */       }
/* 264:302 */       localVelocityContext.put("classname", this.d.isUseFullyQualifiedName() ? localClassElement.getQualifiedName() : localClassElement.getName());
/* 265:303 */       localVelocityContext.put("FQClassname", localClassElement.getQualifiedName());
/* 266:305 */       if (a.isDebugEnabled()) {
/* 267:305 */         a.debug("Velocity Macro:\n" + paramString);
/* 268:    */       }
/* 269:308 */       VelocityEngine localVelocityEngine = VelocityFactory.getVelocityEngine();
/* 270:309 */       a.debug("Executing velocity +++ START +++");
/* 271:310 */       localVelocityEngine.evaluate(localVelocityContext, localStringWriter, getClass().getName(), paramString);
/* 272:311 */       a.debug("Executing velocity +++ END +++");
/* 273:314 */       if (localVelocityContext.get("autoImportPackages") != null) {
/* 274:315 */         paramMap.put("autoImportPackages", (String)localVelocityContext.get("autoImportPackages"));
/* 275:    */       }
/* 276:    */     }
/* 277:    */     catch (Exception localException)
/* 278:    */     {
/* 279:319 */       throw new GenerateCodeException("Error in Velocity code generator", localException);
/* 280:    */     }
/* 281:322 */     return localStringWriter.getBuffer().toString();
/* 282:    */   }
/* 283:    */   
/* 284:    */   public static void executeGenerateActionLater(PsiClass paramPsiClass, final Editor paramEditor, final Collection<PsiMember> paramCollection, final TemplateResource paramTemplateResource, final boolean paramBoolean)
/* 285:    */   {
/* 286:338 */     Runnable local1 = new Runnable()
/* 287:    */     {
/* 288:    */       public void run()
/* 289:    */       {
/* 290:340 */         ApplicationManager.getApplication().runWriteAction(new Runnable()
/* 291:    */         {
/* 292:    */           public void run()
/* 293:    */           {
/* 294:    */             try
/* 295:    */             {
/* 296:343 */               new GenerateToStringWorker(GenerateToStringWorker.1.this.val$clazz, GenerateToStringWorker.1.this.val$editor, GenerateToStringWorker.1.this.val$insertAtOverride).execute(GenerateToStringWorker.1.this.val$selectedMembers, GenerateToStringWorker.1.this.val$template);
/* 297:    */             }
/* 298:    */             catch (Exception localException)
/* 299:    */             {
/* 300:346 */               GenerateToStringUtils.handleException(GenerateToStringWorker.1.this.val$clazz.getProject(), localException);
/* 301:    */             }
/* 302:    */           }
/* 303:    */         });
/* 304:    */       }
/* 305:352 */     };
/* 306:353 */     CommandProcessor.getInstance().executeCommand(paramPsiClass.getProject(), local1, "GenerateToString", null);
/* 307:    */   }
/* 308:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.generate.tostring.GenerateToStringWorker

 * JD-Core Version:    0.7.0.1

 */